package com.cui.project.controller;

import com.api.common.common.BaseResponse;
import com.api.common.common.ErrorCode;
import com.api.common.common.ResultUtils;
import com.api.common.constant.CommonConstant;
import com.api.common.model.entity.InterfaceInfoLog;
import com.api.common.model.entity.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cui.project.service.InterfaceInfoLogService;
import com.cui.project.constant.PageConstants;
import com.cui.project.exception.BusinessException;
import com.cui.project.model.dto.InferfaceInfoLog.InterfaceInfoLogQueryRequest;
import com.cui.project.model.vo.InterfaceInfoLogListVo;
import com.cui.project.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子接口
 *
 * @author cui
 */
@RestController
@RequestMapping("/interface_info_log")
@Slf4j
public class InterfaceInfoLogController {

    @Resource
    private InterfaceInfoLogService interfaceInfoLogService;

    @Resource
    private UserService userService;

    // region 增删改查

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<InterfaceInfoLog> getInterfaceInfoLogById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        InterfaceInfoLog post = interfaceInfoLogService.getById(id);
        return ResultUtils.success(post);
    }

    /**
     * 分页获取列表
     *
     * @param postQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<InterfaceInfoLog>> listInterfaceInfoLogByPage(InterfaceInfoLogQueryRequest postQueryRequest, HttpServletRequest request) {
        if (postQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        InterfaceInfoLog postQuery = new InterfaceInfoLog();
        BeanUtils.copyProperties(postQueryRequest, postQuery);
        long current = postQueryRequest.getCurrent();
        long size = postQueryRequest.getPageSize();
        String sortField = postQueryRequest.getSortField();
        String sortOrder = postQueryRequest.getSortOrder();
        // 限制爬虫
        if (size > PageConstants.MAX_PAGE_SIZE) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<InterfaceInfoLog> queryWrapper = new QueryWrapper<>(postQuery);
        queryWrapper.orderBy(StringUtils.isNotBlank(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        Page<InterfaceInfoLog> postPage = interfaceInfoLogService.page(new Page<>(current, size), queryWrapper);
        return ResultUtils.success(postPage);
    }

    /**
     * 接口调用得历史统计和历史列表
     *
     * @param interfaceInfoId
     * @param request
     * @return
     */
    @GetMapping("/list/history")
    public BaseResponse<InterfaceInfoLogListVo> listInterfaceInfoLogHistoryById(Long interfaceInfoId, HttpServletRequest request) {
        if (interfaceInfoId == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 限制爬虫
//        if (size > PageConstants.MAX_PAGE_SIZE) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        QueryWrapper<InterfaceInfoLog> queryWrapper = new QueryWrapper<>(postQuery);
//        queryWrapper.orderBy(StringUtils.isNotBlank(sortField),
//                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
//        Page<InterfaceInfoLog> postPage = interfaceInfoLogService.page(new Page<>(current, size), queryWrapper);


        User loginUser = userService.getLoginUser(request);
        InterfaceInfoLogListVo interfaceInfoLogListVo =  interfaceInfoLogService.listInterfaceInfoLogHistoryById(interfaceInfoId,loginUser);
        return ResultUtils.success(interfaceInfoLogListVo);
    }
    // endregion

}
